import { Form } from 'antd';
import React, { useMemo } from 'react';
/***
 * form field的统一入口
 */
export const CommonField = React.memo(params => {
    const { Component, props: _props, ...args } = params;
    const props = useMemo(() => {
        if (typeof _props === 'function') {
            const formApi = Form.useFormInstance();
            return _props(formApi);
        }
        return _props;
    }, [_props]);
    /**
     * form.item会注入onChange方法，所以这里需要做一层代理
    */
    const mergeProps = useMemo(() => {
        return {
            ...props,
            ...Object.keys(args).reduce((acc, key) => {
                if (typeof args[key] === 'function' && typeof props?.[key] === 'function') {
                    acc[key] = (...params) => {
                        props[key](...params);
                        return args[key](...params);
                    };
                }
                else {
                    acc[key] = args[key];
                }
                return acc;
            }, {})
        };
    }, [args, props]);
    return React.createElement(Component, mergeProps);
});
